package com.work.dao;

import com.work.pojo.CarInfo;
import com.work.pojo.Goods;
import com.work.util.DBUtil;

import java.util.List;

public class CarDaoImpl extends DBUtil implements CarDao {

    @Override
    public int insertNewGoodsCar(int user_id, int goods_num, int goods_id) {
        String sql="insert into car values (?,?,?,?)";
        return super.executeUpdate(sql,user_id,goods_num,goods_id,user_id);
    }

    @Override
    public int insertRepeatGoodsCar(int user_id, int goods_id, int goods_num) {
        String sql="UPDATE car SET goods_num=? WHERE cart_id=? AND goods_id=?";
        return super.executeUpdate(sql,goods_num,user_id,goods_id);
    }

    @Override
    public List<CarInfo> queryCarInfo(int user_id) {
        String sql="SELECT goods.`goods_id`,goods.`goods_name`,\n" +
                "        goods.`goods_price`,car.`goods_num`\n" +
                "        FROM `user`\n" +
                "        INNER JOIN goods\n" +
                "        INNER JOIN car\n" +
                "        ON user.`user_id`=car.`user_id`\n" +
                "        AND goods.`goods_id`=car.`goods_id`  " +
                "         WHERE user.`user_id`=?";
        return super.executeQueryList(sql,CarInfo.class,user_id);
    }

    //通过good_id在购物车中查询 若没有则为新商品加入
    @Override
    public Goods queryByGoods_idInCar(int user_id, int goods_id) {
        String sql="select * from car where user_id=?,goods_id=?";
        return super.executeQueryOne(sql,Goods.class,user_id,goods_id);
    }

    @Override
    public long GoodsNum() {
        String sql=" SELECT COUNT(goods_id) FROM car";
        return (long) super.executeQueryNumber(sql);
    }

    @Override
    public int clearCar() {
        String sql="delete from car";
        return 0;
    }
}
